package com.dx.mes.service.special.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.dx.common.table.ppt.*;
import com.dx.mes.common.dao.*;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.mes.common.table.rtnBean.NotFoundRtnBeen;
import com.dx.mes.common.table.rtnBean.SimpleRtnBeen;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.special.service.IFspboxchgService;
import com.dx.mes.facade.special.tx.Fspboxchg.FspboxchgI;
import com.dx.mes.facade.special.tx.Fspboxchg.FspboxchgIA;
import com.dx.mes.facade.special.tx.Fspboxchg.FspboxchgIB;
import com.dx.mes.facade.special.tx.Fspboxchg.FspboxchgO;
import com.dx.mes.facade.special.tx.Fspboxchg.FspboxchgOA;
import com.dx.mes.facade.special.tx.Fspboxchg.FspboxchgOB;

import static com.dx.mes.common.bean.def.GenericDef.*;

@Service("fspboxchgService")
@SuppressWarnings({"unchecked"})
public class FspboxchgService implements IFspboxchgService{
	private LogUtils logUtils;
	private static String TX_NAME = T_FSPBOXCHG;

	@Autowired
	private IRetBoxDao retBoxDao;
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IBisDataDao bisDataDao;
	@Autowired
	private IRetWODao retWODao;
	@Autowired
	private IBisMdlDefDao bisMdlDefDao;

	public String subMainProc(String evt_no,String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(FspboxchgService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FspboxchgService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");

		Session session = retPrdInfoDao.getCrSession();
		long rtnCode = _NORMAL;
		String outMsg = "";

		FspboxchgI inTrx = new FspboxchgI();
		FspboxchgO outTrx = new FspboxchgO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		outTrx.setType_id(TRX_OUT);

		try {
			inTrx = JacksonUtil.fromJson(strInMsg, FspboxchgI.class);

			rtnCode = subMainProc2(inTrx, outTrx);
			if (rtnCode != _NORMAL) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				outTrx.setRtn_code(String.valueOf(rtnCode));
			} else {
				session.flush();
			}
		} catch (Exception ex) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logUtils.error(Util.stackTraceToString(ex));
		} finally {
			outMsg = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + outMsg + "]");
		}
		return outMsg;
	}

	/**
	 * Real action type
	 *
	 * @param //actionType
	 * @return
	 */
	private long subMainProc2(FspboxchgI inTrx, FspboxchgO outTrx) {
		char actionType = inTrx.getAction_flg().toCharArray()[0];
		long lRc = _NORMAL;
		switch (actionType) {
			case 'C': {
				lRc = boxChgFunc(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'Q':
				lRc=queryFnc(inTrx,outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			case 'A':
				lRc=queryFncA(inTrx,outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			case 'B':
				lRc=queryFncB(inTrx,outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;

			case 'S':
				lRc=saveFncA(inTrx,outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;

			default: {
				outTrx.setRtn_mesg("Invalide type input: [" + actionType + "]");
				return E_FSPBOXCHG_INVALID_INPUT;
			}
		}

		return lRc;
	}


	public FspboxchgO queryA(Ret_box ret_box,FspboxchgO outTrx){
		//boxSetCode 是卡匣设定类型，此处不需要
		/*String boxSetCode = ret_box.getBox_set_code();
		String hql = "FROM Bis_data where 1 =1 ";
		if(boxSetCode!=null){
			hql =hql + " and data_id = '" + boxSetCode +"'";
		}
		List<Bis_data> bis_datas=bisDataDao.find(hql);
		String ext_1 = bis_datas.get(0).getExt_1();*/
		//根据产品找出最大装箱数量
		Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(ret_box.getMdl_id_fk());
		Integer ext1=120;
		if( bis_mdl_def!=null){
			 ext1 = Integer.valueOf(bis_mdl_def.getBox_cnt());
		}
		//这个set要判断
		outTrx.setTbl_cntA(ext1);
		String hql2 = "FROM Ret_prd_info where 1=1 ";
		String boxId = ret_box.getBox_id();
		if(boxId!=null && !boxId.equals("")){
			hql2 =hql2 + " and box_id_fk= '" +boxId +"'";
		}
		List<Ret_prd_info> ret_prd_infos = retPrdInfoDao.find(hql2);
		List<FspboxchgOA> oaryListA= new ArrayList<FspboxchgOA>();
		//Integer prdQty =ret_box.getPrd_qty();
		for (Ret_prd_info ret_prd_info : ret_prd_infos) {
			FspboxchgOA fspboxchgOA = new FspboxchgOA();
			fspboxchgOA.setBox_id_fk(ret_prd_info.getBox_id_fk());
			fspboxchgOA.setPrd_stat(ret_prd_info.getPrd_stat());
			//slot_no ：String类型--01、03
			fspboxchgOA.setSlot_no(ret_prd_info.getSlot_no());
			fspboxchgOA.setMdl_id_fk(ret_prd_info.getMdl_id_fk());
			fspboxchgOA.setPrd_grade(ret_prd_info.getPrd_grade());
			fspboxchgOA.setPrd_qty(ret_prd_info.getPrd_qty());
			fspboxchgOA.setPrd_seq_id(ret_prd_info.getPrd_seq_id());
			oaryListA.add(fspboxchgOA);
		}

		//1.数据库里没有存子格位的也需要添加到list中，子格位的最大数为ext1--20
		List<FspboxchgOA> oaryList = new ArrayList<FspboxchgOA>();
		for(int i = 1;i < ext1 + 1; i++){
			FspboxchgOA fspboxchgOA = new FspboxchgOA();
			for(int j = 0; j < oaryListA.size(); j++){
				String slot_no = oaryListA.get(j).getSlot_no();
				slot_no = slot_no.replaceFirst("^0*", "");
				Integer slotNo = Integer.parseInt(slot_no);
				if(slotNo.equals(i)){
					fspboxchgOA.setBox_id_fk(oaryListA.get(j).getBox_id_fk());
					fspboxchgOA.setPrd_stat(oaryListA.get(j).getPrd_stat());
					String slotno = String.format("%02d", slotNo);
					fspboxchgOA.setSlot_no(slotno);
					fspboxchgOA.setMdl_id_fk(oaryListA.get(j).getMdl_id_fk());
					fspboxchgOA.setPrd_grade(oaryListA.get(j).getPrd_grade());
					fspboxchgOA.setPrd_qty(oaryListA.get(j).getPrd_qty());
					fspboxchgOA.setPrd_seq_id(oaryListA.get(j).getPrd_seq_id());
					break;
				}else{//不相等继续循环，但是不设值
					fspboxchgOA.setBox_id_fk("");
					fspboxchgOA.setPrd_stat("");
					//子格位跟i有关
					String slotno = String.format("%02d", i);
					fspboxchgOA.setSlot_no(slotno);
					fspboxchgOA.setMdl_id_fk("");
					fspboxchgOA.setPrd_grade("");
					fspboxchgOA.setPrd_seq_id("");
					continue;
				}
			}
			oaryList.add(fspboxchgOA);
		}
		outTrx.setOaryA(oaryList);
		return outTrx;
	}

	public FspboxchgO queryB(Ret_box ret_box,FspboxchgO outTrx){
		//boxSetCode 是卡匣设定类型，此处不需要
		/*String boxSetCode = ret_box.getBox_set_code();
		String hql = "FROM Bis_data where 1 =1 ";
		if(boxSetCode!=null){
			hql =hql + " and data_id = '" + boxSetCode +"'";
		}
		List<Bis_data> bis_datas=bisDataDao.find(hql);
		String ext_1 = bis_datas.get(0).getExt_1();*/
		//根据产品找出最大装箱数量
		Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(ret_box.getMdl_id_fk());
		Integer ext1=120;
		if(bis_mdl_def!=null){
			ext1 = Integer.valueOf(bis_mdl_def.getBox_cnt());
		}
		//这个set要判断
		outTrx.setTbl_cntB(ext1);
		String hql2 = "FROM Ret_prd_info where 1=1 ";
		String boxId = ret_box.getBox_id();
		if(boxId!=null && !boxId.equals("")){
			hql2 =hql2 + " and box_id_fk= '" +boxId +"'";
		}
		List<Ret_prd_info> ret_prd_infos = retPrdInfoDao.find(hql2);
		List<FspboxchgOB> oaryListB= new ArrayList<FspboxchgOB>();
		//Integer prdQty =ret_box.getPrd_qty();
		if(!ret_prd_infos.isEmpty()){
			for (Ret_prd_info ret_prd_info : ret_prd_infos) {
				FspboxchgOB fspboxchgOB = new FspboxchgOB();
				fspboxchgOB.setBox_id_fk(ret_prd_info.getBox_id_fk());
				fspboxchgOB.setPrd_stat(ret_prd_info.getPrd_stat());
				//slot_no ：String类型--001、003
				fspboxchgOB.setSlot_no(ret_prd_info.getSlot_no());
				fspboxchgOB.setMdl_id_fk(ret_prd_info.getMdl_id_fk());
				fspboxchgOB.setPrd_grade(ret_prd_info.getPrd_grade());
				//Integer prdQtys = prdQty * ret_prd_info.getPrd_qty();
				fspboxchgOB.setPrd_qty(ret_prd_info.getPrd_qty());
				fspboxchgOB.setPrd_seq_id(ret_prd_info.getPrd_seq_id());
				oaryListB.add(fspboxchgOB);
			}
		}

		//1.数据库里没有存子格位的也需要添加到list中，子格位的最大数为ext1--20
		List<FspboxchgOB> oaryList = new ArrayList<FspboxchgOB>();
		if(oaryListB.isEmpty()){
			for(int i = 1; i <ext1 + 1;i++){
				FspboxchgOB fspboxchgOB = new FspboxchgOB();
				fspboxchgOB.setBox_id_fk("");
				fspboxchgOB.setPrd_stat("");
				//子格位跟i有关
				String slotno = String.format("%02d", i);
				fspboxchgOB.setSlot_no(slotno);
				fspboxchgOB.setMdl_id_fk("");
				fspboxchgOB.setPrd_grade("");
				fspboxchgOB.setPrd_seq_id("");
				oaryList.add(fspboxchgOB);
			}
		}else{
			for(int i = 1;i < ext1 + 1; i++){
				FspboxchgOB fspboxchgOB = new FspboxchgOB();
				//外部循环20次没有问题
				for(int j = 0; j < oaryListB.size(); j++){
					String slot_no = oaryListB.get(j).getSlot_no();
					slot_no = slot_no.replaceFirst("^0*", "");
					Integer slotNo = Integer.parseInt(slot_no);
					//如果相等则赋值
					if(slotNo.equals(i)){
						fspboxchgOB.setBox_id_fk(oaryListB.get(j).getBox_id_fk());
						fspboxchgOB.setPrd_stat(oaryListB.get(j).getPrd_stat());
						String slotno = String.format("%02d", slotNo);
						fspboxchgOB.setSlot_no(slotno);
						fspboxchgOB.setMdl_id_fk(oaryListB.get(j).getMdl_id_fk());
						fspboxchgOB.setPrd_grade(oaryListB.get(j).getPrd_grade());
						fspboxchgOB.setPrd_qty(oaryListB.get(j).getPrd_qty());
						fspboxchgOB.setPrd_seq_id(oaryListB.get(j).getPrd_seq_id());
						break;
					}else{//不相等继续循环，但是不设值
						fspboxchgOB.setBox_id_fk("");
						fspboxchgOB.setPrd_stat("");
						//子格位跟i有关
						String slotno = String.format("%02d", i);
						fspboxchgOB.setSlot_no(slotno);
						fspboxchgOB.setMdl_id_fk("");
						fspboxchgOB.setPrd_grade("");
						fspboxchgOB.setPrd_seq_id("");
						continue;
					}
				}
				oaryList.add(fspboxchgOB);
			}
		}

		outTrx.setOaryB(oaryList);
		return outTrx;
	}

	public SimpleRtnBeen checkBox(FspboxchgI inTrx,FspboxchgO outTrx){
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		long lRc = _NORMAL;
		String gszMsgBuf = "";
		Ret_box gOld_box = new Ret_box();
		Ret_box gNew_box = new Ret_box();
		//输入检查方法（检查输入是否为空）
		rtnBeen = inputChk(inTrx);
		if (_NORMAL != rtnBeen.getlRc()) {
			gszMsgBuf = rtnBeen.getMsgBuf();
			outTrx.setRtn_mesg(gszMsgBuf);
			return rtnBeen;
		}
		// 查询并检查原箱
		rtnBeen = chkOldBox(inTrx);
		if (_NORMAL != rtnBeen.getlRc()) {
			gszMsgBuf = rtnBeen.getMsgBuf();
			outTrx.setRtn_mesg(gszMsgBuf);
			return rtnBeen;
		}else{
			gOld_box = (Ret_box) rtnBeen.getObj();
		}

		// 查询并检查右侧箱
		rtnBeen = chkNewBox(inTrx, gOld_box);
		if (_NORMAL != rtnBeen.getlRc()) {
			gszMsgBuf = rtnBeen.getMsgBuf();
			outTrx.setRtn_mesg(gszMsgBuf);
			return rtnBeen;
		}else{
			gNew_box = (Ret_box) rtnBeen.getObj();
		}
		return rtnBeen;
	}




	//同时查询2个箱子
	public long queryFnc(FspboxchgI inTrx,FspboxchgO outTrx) {
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		long lRc = _NORMAL;
		String gszMsgBuf = "";
		Ret_box gOld_box = new Ret_box();
		Ret_box gNew_box = new Ret_box();
		//输入检查方法（检查输入是否为空）
		rtnBeen = inputChk(inTrx);
		if (_NORMAL != rtnBeen.getlRc()) {
			gszMsgBuf = rtnBeen.getMsgBuf();
			outTrx.setRtn_mesg(gszMsgBuf);
			return rtnBeen.getlRc();
		}
		// 查询并检查原箱
		rtnBeen = chkOldBox(inTrx);
		if (_NORMAL != rtnBeen.getlRc()) {
			gszMsgBuf = rtnBeen.getMsgBuf();
			outTrx.setRtn_mesg(gszMsgBuf);
			return rtnBeen.getlRc();
		}else{
			gOld_box = (Ret_box) rtnBeen.getObj();
		}

		// 查询并检查右侧箱
		rtnBeen = chkNewBox(inTrx, gOld_box);
		if (_NORMAL != rtnBeen.getlRc()) {
			gszMsgBuf = rtnBeen.getMsgBuf();
			outTrx.setRtn_mesg(gszMsgBuf);
			return rtnBeen.getlRc();
		}else{
			gNew_box = (Ret_box) rtnBeen.getObj();
		}
		//调
		queryA(gOld_box, outTrx);
		queryB(gNew_box, outTrx);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return lRc ;
	}

	//单个检查A卡匣
	public long queryFncA(FspboxchgI inTrx,FspboxchgO outTrx) {
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		long lRc = _NORMAL;
		String gszMsgBuf = "";
		Ret_box gOld_box = new Ret_box();
		//输入检查方法（检查输入是否为空）
		rtnBeen = inputChkA(inTrx);
		if (_NORMAL != lRc) {
			gszMsgBuf = rtnBeen.getMsgBuf();
			outTrx.setRtn_mesg(gszMsgBuf);
			return rtnBeen.getlRc();
		}
		// 查询并检查原箱
		rtnBeen = chkOldBox(inTrx);
		if (_NORMAL != rtnBeen.getlRc()) {
			gszMsgBuf = rtnBeen.getMsgBuf();
			outTrx.setRtn_mesg(gszMsgBuf);
			return rtnBeen.getlRc();
		}else{
			gOld_box = (Ret_box) rtnBeen.getObj();
		}

		queryA(gOld_box, outTrx);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return lRc ;
	}

	//单个检查B卡匣
	public long queryFncB(FspboxchgI inTrx,FspboxchgO outTrx) {
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		long lRc = _NORMAL;
		String gszMsgBuf = "";
		Ret_box gNew_box = new Ret_box();
		//输入检查方法（检查输入是否为空）
		rtnBeen = inputChkB(inTrx);
		if (_NORMAL != lRc) {
			gszMsgBuf = rtnBeen.getMsgBuf();
			outTrx.setRtn_mesg(gszMsgBuf);
			return rtnBeen.getlRc();
		}
		// 查询并检查右侧箱
		rtnBeen = chkNewBoxB(inTrx);
		if (_NORMAL != rtnBeen.getlRc()) {
			gszMsgBuf = rtnBeen.getMsgBuf();
			outTrx.setRtn_mesg(gszMsgBuf);
			return rtnBeen.getlRc();
		}else{
			gNew_box = (Ret_box) rtnBeen.getObj();
		}
		//调
		queryB(gNew_box, outTrx);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return lRc ;
	}

	//保存A卡匣玻璃信息
	public long saveFncA(FspboxchgI inTrx,FspboxchgO outTrx) {
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		//SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		long lRc = _NORMAL;
		//String gszMsgBuf = "";
		Timestamp gEvtTimestamp = Util.getCurrentTimestamp();
		String boxIdA =inTrx.getBox_idA();
		List<FspboxchgIA> fspboxchgIAs=inTrx.getIaryA();
		FspboxchgIA fbc1 = fspboxchgIAs.get(0);
		FspboxchgIA fbc2 = fspboxchgIAs.get(1);

		String slotId1 = fbc1.getSlot_no();
		String slotId2 = fbc2.getSlot_no();
		String prd1 = fbc1.getPrd_seq_id();
		String prd2 = fbc2.getPrd_seq_id();

		Ret_prd_info ret_prd_info1=retPrdInfoDao.get(prd1);
		if(ret_prd_info1 == null){
			outTrx.setRtn_code(E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("找不到玻璃号为["+prd1+"]的玻璃！");
			return 1;
		}

		Ret_prd_info ret_prd_info2=retPrdInfoDao.get(prd2);
		if(ret_prd_info2 == null){
			outTrx.setRtn_code(E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("找不到玻璃号为["+prd2+"]的玻璃！");
			return 1;
		}

		String boxId1 = ret_prd_info1.getBox_id_fk();
		String boxId2 = ret_prd_info2.getBox_id_fk();
		if(!boxIdA.equals(boxId1) || !boxIdA.equals(boxId2)){
			//。code要重新添加
			outTrx.setRtn_code(E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("两片玻璃的卡匣ID不同，请重新选择！");
			return 1;
		}

		//交换solotid
		if(slotId1 !=null && slotId1.length()!=0){
			ret_prd_info2.setSlot_no(slotId1);
			ret_prd_info2.setEvt_cate(EVT_SLOT_NO_CHANGE);
			ret_prd_info2.setEvt_usr(inTrx.getEvt_usr());
			ret_prd_info2.setEvt_timestamp(gEvtTimestamp);
			ret_prd_info2.setSht_ope_msg(inTrx.getRemark());
			retPrdInfoDao.update(ret_prd_info2);
		}

		if(slotId2 !=null && slotId2.length()!=0){
			ret_prd_info1.setSlot_no(slotId2);
			ret_prd_info1.setEvt_cate(EVT_SLOT_NO_CHANGE);
			ret_prd_info1.setEvt_usr(inTrx.getEvt_usr());
			ret_prd_info1.setEvt_timestamp(gEvtTimestamp);
			ret_prd_info1.setSht_ope_msg(inTrx.getRemark());
			retPrdInfoDao.update(ret_prd_info1);
		}

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return lRc;
	}

	/**
	 * 检查输入数据
	 *
	 * @return
	 */
	private SimpleRtnBeen inputChk(FspboxchgI inTrx) {
		String gszMsgBuf = "";
		/*if (Util.isSpaceCheck(inTrx.getBox_idA())) {
			gszMsgBuf = "右侧箱号不能为空![" + inTrx.getBox_idA() + "]";
			return new SimpleRtnBeen(E_FSPBOXCHG_NEW_BOX_ID_NULL, gszMsgBuf);
		}
		if (Util.isSpaceCheck(inTrx.getBox_idB())) {
			gszMsgBuf = "左侧箱号不能为空![" +inTrx.getBox_idB() + "]";
			return new SimpleRtnBeen(E_FSPBOXCHG_OLD_BOX_ID_NULL, gszMsgBuf);
		}*/
		if(inTrx.getBox_idA().equals(inTrx.getBox_idB())){
			gszMsgBuf = "两侧箱号不能相同！";
			return new SimpleRtnBeen(E_FSPBOXCHG_OLD_BOX_ID_NULL, gszMsgBuf);
		}

		return new SimpleRtnBeen(_NORMAL);
	}

	private SimpleRtnBeen inputChkA(FspboxchgI inTrx) {
		String gszMsgBuf = "";
		if (Util.isSpaceCheck(inTrx.getBox_idA())) {
			gszMsgBuf = "右侧箱号不能为空![" + inTrx.getIaryA().get(0).getBox_id_fk() + "]";
			return new SimpleRtnBeen(E_FSPBOXCHG_NEW_BOX_ID_NULL, gszMsgBuf);
		}
		return new SimpleRtnBeen(_NORMAL);
	}

	private SimpleRtnBeen inputChkB(FspboxchgI inTrx) {
		String gszMsgBuf = "";
		/*if (Util.isSpaceCheck(inTrx.getBox_idB())) {
			gszMsgBuf = "左侧箱号不能为空![" + inTrx.getIaryB().get(0).getBox_id_fk() + "]";
			return new SimpleRtnBeen(E_FSPBOXCHG_OLD_BOX_ID_NULL, gszMsgBuf);
		}*/

		return new SimpleRtnBeen(_NORMAL);
	}
	/**
	 * 交换箱
	 *
	 * @return
	 */
	private long boxChgFunc(FspboxchgI inTrx, FspboxchgO outTrx) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		long lRc = _NORMAL;
		String gszMsgBuf = "";
		Ret_box gOld_box = new Ret_box();
		Ret_box gNew_box = new Ret_box();
		List<Ret_prd_info> gOld_prdList = new ArrayList<Ret_prd_info>();
		List<Ret_prd_info> gNew_prdList = new ArrayList<Ret_prd_info>();

		//check2个箱子状态
		rtnBeen = checkBox(inTrx,outTrx);
		if(_NORMAL != rtnBeen.getlRc()){
			gszMsgBuf = rtnBeen.getMsgBuf();
			outTrx.setRtn_mesg(gszMsgBuf);
			return rtnBeen.getlRc();
		}


		List<FspboxchgIA> iaryA =inTrx.getIaryA();
		Iterator<FspboxchgIA> itA = iaryA.iterator();
		while(itA.hasNext()){
			FspboxchgIA fspboxchgIA = itA.next();
			String boxIdFkA =fspboxchgIA.getBox_id_fk();
			if(boxIdFkA.length()==0){
				itA.remove();
			}
		}
		inTrx.setIaryA(iaryA);

		List<FspboxchgIB> iaryB =inTrx.getIaryB();
		Iterator<FspboxchgIB> itB = iaryB.iterator();
		while(itB.hasNext()){
			FspboxchgIB fspboxchgIB = itB.next();
			String boxIdFkB =fspboxchgIB.getBox_id_fk();
			if(boxIdFkB.length()==0){
				itB.remove();
			}
		}
		inTrx.setIaryB(iaryB);

		//需要箱子信息
		String boxIdA=inTrx.getBox_idA();
		gOld_box =retBoxDao.get(boxIdA);

		String boxIdB=inTrx.getBox_idB();
		gNew_box = retBoxDao.get(boxIdB);

	/*	//检查prd_seq_id 跨制程
		lRc = checkPrdQrsProc(inTrx, outTrx);
		if(lRc != _NORMAL){
			return lRc;
		}*/

		// 查询并检查原箱prd信息
		rtnBeen = chkOldPrdInfo(inTrx, gOld_box);
		if (_NORMAL != rtnBeen.getlRc()) {
			gszMsgBuf = rtnBeen.getMsgBuf();
			outTrx.setRtn_mesg(gszMsgBuf);
			return rtnBeen.getlRc();
		}else{
			gOld_prdList = (List<Ret_prd_info>) rtnBeen.getObj();
		}

		// 查询并检查右侧箱prd信息
		rtnBeen = chkNewPrdInfo(inTrx, gNew_box);
		if (_NORMAL != rtnBeen.getlRc()) {
			gszMsgBuf = rtnBeen.getMsgBuf();
			outTrx.setRtn_mesg(gszMsgBuf);
			return rtnBeen.getlRc();
		}else{
			gNew_prdList = (List<Ret_prd_info>) rtnBeen.getObj();
		}

		//更新prdinfo信息  box_id_fk \slot_no
		rtnBeen = updateA(inTrx);
		if (_NORMAL != rtnBeen.getlRc()) {
			return lRc;
		}

		rtnBeen =updateB(inTrx);
		if (_NORMAL != rtnBeen.getlRc()) {
			return lRc;
		}

		// 更新Prd信息
		lRc = realChgPrdInfo(inTrx, gOld_prdList, gNew_prdList);
		if (_NORMAL != lRc) {
			return lRc;
		}

		// 配置Box信息
		lRc = configBoxProc(gOld_box, gNew_box, outTrx);
		if (_NORMAL != lRc) {
			return lRc;
		}

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return _NORMAL;
	}

	private SimpleRtnBeen updateA(FspboxchgI inTrx){
		//long lRc= _NORMAL;
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		Timestamp gEvtTimestamp = Util.getCurrentTimestamp();
		List<FspboxchgIA> fspboxchgIAs = inTrx.getIaryA();
		//更新A
		List<Ret_prd_info> ret_prd_infos = new ArrayList<Ret_prd_info>();
		for (FspboxchgIA fspboxchgIA : fspboxchgIAs) {
			String boxIdFk = fspboxchgIA.getBox_id_fk();
			String slotNo = fspboxchgIA.getSlot_no();
			String prdSeqId = fspboxchgIA.getPrd_seq_id();
			Ret_prd_info ret_prd_info = new Ret_prd_info();
			ret_prd_info = retPrdInfoDao.get(prdSeqId);
			ret_prd_info.setBox_id_fk(inTrx.getBox_idA());
			ret_prd_info.setSlot_no(slotNo);
			ret_prd_info.setEvt_cate(EVT_BOX_CHANGE);
			ret_prd_info.setEvt_usr(inTrx.getEvt_usr());
			ret_prd_info.setEvt_timestamp(gEvtTimestamp);
			ret_prd_info.setSht_ope_msg(inTrx.getRemark());
			ret_prd_infos.add(ret_prd_info);
		}
		if(!ret_prd_infos.isEmpty()){
			retPrdInfoDao.saveOrUpdateAll(ret_prd_infos);
		}
		rtnBeen.setObj(fspboxchgIAs);
		return rtnBeen;
	}

	private SimpleRtnBeen updateB (FspboxchgI inTrx){
		//long lRc= _NORMAL;
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		Timestamp gEvtTimestamp = Util.getCurrentTimestamp();
		List<FspboxchgIB> fspboxchgIBs=inTrx.getIaryB();
		//更新B
		List<Ret_prd_info> ret_prd_infos = new ArrayList<Ret_prd_info>();
		for (FspboxchgIB fspboxchgIB : fspboxchgIBs) {
			String boxIdFk = fspboxchgIB.getBox_id_fk();
			String slotNo = fspboxchgIB.getSlot_no();
			String prdSeqId = fspboxchgIB.getPrd_seq_id();
			Ret_prd_info ret_prd_info = new Ret_prd_info();
			ret_prd_info = retPrdInfoDao.get(prdSeqId);
			ret_prd_info.setBox_id_fk(inTrx.getBox_idB());
			ret_prd_info.setSlot_no(slotNo);
			ret_prd_info.setEvt_cate(EVT_BOX_CHANGE);
			ret_prd_info.setEvt_usr(inTrx.getEvt_usr());
			ret_prd_info.setEvt_timestamp(gEvtTimestamp);
			ret_prd_info.setSht_ope_msg(inTrx.getRemark());
			ret_prd_infos.add(ret_prd_info);
		}
		if(!ret_prd_infos.isEmpty()){
			retPrdInfoDao.saveOrUpdateAll(ret_prd_infos);
		}
		rtnBeen.setObj(fspboxchgIBs);
		return rtnBeen;
	}

	/*//置空卡匣
	public void checkEmpty(Ret_box ret_box){
		List<Ret_prd_info> gRet_prd_infoList = new ArrayList<Ret_prd_info>();
		gRet_prd_infoList = retPrdInfoDao.getShtListInfo(ret_box.getBox_id());
		if(null == gRet_prd_infoList){
			retBoxHibernateDao.setBoxEmpt(ret_box);
		}
	}
	*/
	/**
	 * 检查原箱信息
	 *
	 * @return SimpleRtnBeen{Ret_box}
	 */
	private SimpleRtnBeen chkOldBox(FspboxchgI inTrx) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		Ret_box gOld_box = retBoxDao.getWithLock(inTrx.getBox_idA(),
				LockMode.UPGRADE);
		if (null == gOld_box) {
			return new SimpleRtnBeen(E_RET_BOX + E_READ_NOT_FOUND,
					new NotFoundRtnBeen(inTrx.getBox_idA(), Ret_box.class)
							.toString());
		}
		rtnBeen.setObj(gOld_box);

		// Status check
		if (PROC_WAIT.equals(gOld_box.getBox_stat()) || PROC_COMPLETE.equals(gOld_box.getBox_stat()) || DENSE_STAT_EMPTY.equals(gOld_box.getBox_stat())||
				(PROC_SHIP.equals(gOld_box.getBox_stat()) && !Util.isSpaceCheck(inTrx.getIn_bank()) && inTrx.getIn_bank().equals(_YES))) {
			;
		} else {
			return new SimpleRtnBeen(E_FSPBOXCHG_INVALID_OLD_BOX_STAT, "左侧箱号 ["
					+ inTrx.getBox_idA() + "]状态为 [" + gOld_box.getBox_stat()
					+ "],状态无效!");
		}

		// Check real empt
		if (!PROC_EMPTY.equals(gOld_box.getBox_stat()) && EXIST_CARRIER.equals(gOld_box.getReal_emp())) {
			// 非空BOX
		} else if (PROC_EMPTY.equals(gOld_box.getBox_stat()) && EMPTY_CARRIER.equals(gOld_box.getReal_emp())) {
			// 真空BOX
		} else {
			return new SimpleRtnBeen(E_FSPBOXCHG_OLD_BOX_REAL_EMPT_CHK_NG,
					"左侧箱号真空状态检查NG, 箱子状态是[" + gOld_box.getBox_stat()
							+ "], 但真空标记(real_empt)是[" + gOld_box.getReal_emp() + "]!");
		}
		return rtnBeen;
	}

	/**
	 * 检查右侧箱信息
	 *
	 * @return SimpleRtnBeen{Ret_box}
	 */
	private SimpleRtnBeen chkNewBox(FspboxchgI inTrx, Ret_box gOld_box) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		Ret_box gNew_box = retBoxDao.getWithLock(inTrx.getBox_idB(),
				LockMode.UPGRADE);
		if (null == gNew_box) {
			return new SimpleRtnBeen(E_RET_BOX + E_READ_NOT_FOUND,
					new NotFoundRtnBeen(inTrx.getBox_idB(), Ret_box.class)
							.toString());
		}
		rtnBeen.setObj(gNew_box);

		// Status check
		if (PROC_WAIT.equals(gNew_box.getBox_stat()) || PROC_COMPLETE.equals(gNew_box.getBox_stat()) || PROC_EMPTY.equals(gNew_box.getBox_stat()) ||
			(PROC_SHIP.equals(gNew_box.getBox_stat()) && !Util.isSpaceCheck(inTrx.getIn_bank()) && inTrx.getIn_bank().equals(_YES))) {;
		} else {
			return new SimpleRtnBeen(E_FSPBOXCHG_INVALID_NEW_BOX_STAT, "右侧箱号["
					+ inTrx.getBox_idB()+ "]状态为 [" + gNew_box.getBox_stat()
					+ "],状态无效!");
		}

		// Check real empt
		if (!PROC_EMPTY.equals(gNew_box.getBox_stat())
				&& EXIST_CARRIER.equals(gNew_box.getReal_emp())) {
			;// 非空BOX
		} else if (PROC_EMPTY.equals(gNew_box.getBox_stat())
				&& EMPTY_CARRIER.equals(gNew_box.getReal_emp())) {
			;// 真空BOX
		} else {
			return new SimpleRtnBeen(E_FSPBOXCHG_NEW_BOX_REAL_EMPT_CHK_NG,
					"右侧箱号真空状态检查NG, 箱子状态是[" + gNew_box.getBox_stat()
							+ "], 但真空标记(real_empt)是[" + gNew_box.getReal_emp()
							+ "]!");
		}

//		箱子状态为EMPTY，不卡控工单号
		if (PROC_EMPTY.equals(gNew_box.getBox_stat())
				|| PROC_EMPTY.equals(gOld_box.getBox_stat())){
			return rtnBeen;
		}

		// Check wo
		if(!gOld_box.getWo_id_fk().equals(gNew_box.getWo_id_fk())){
			return new SimpleRtnBeen(
					SHT_WO_ID_MIX,
					"两个箱子的工单号号不同，请确认！" + "左侧箱号["
							+ gOld_box.getBox_id() + "]，工单号["
							+ gOld_box.getWo_id_fk() + "]，状态["
							+ gOld_box.getBox_stat() + "]" + "右侧箱号["
							+ gNew_box.getBox_id() + "]，工单号["
							+ gNew_box.getWo_id_fk() + "]，状态["
							+ gNew_box.getBox_stat() + "]");
		}

		//Check PRD_ADMIN_FLG
		if("2".equals(gOld_box.getBnk_flg()) || "2".equals(gNew_box.getBnk_flg())){
			if(null != gOld_box.getPrd_admin_flg() && null != gNew_box.getPrd_admin_flg()){
				if( (!PROC_EMPTY.equals(gNew_box.getBox_stat())) && (!PROC_EMPTY.equals(gOld_box.getBox_stat()))){
					return new SimpleRtnBeen(
							E_FSPBOXCHG_DIFF_BOX_ADMIN_FLG_WHEN_CHANGE_WO,
							"两个箱子的半成品可出货状态不同，请确认！" + "左侧箱号["
									+ gOld_box.getBox_id() + "]，半成品可出货状态["
									+ gOld_box.getPrd_admin_flg().substring(0,1) + "]，右侧箱号["
									+ gNew_box.getBox_id() + "]，半成品可出货状态["
									+ gNew_box.getPrd_admin_flg().substring(0,1) + "]");
				}
			}
		}
		return rtnBeen;
	}

	private SimpleRtnBeen chkNewBoxB(FspboxchgI inTrx) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		Ret_box gNew_box = retBoxDao.getWithLock(inTrx.getBox_idB(),
				LockMode.UPGRADE);
		if (null == gNew_box) {
			return new SimpleRtnBeen(E_RET_BOX + E_READ_NOT_FOUND,
					new NotFoundRtnBeen(inTrx.getBox_idB(), Ret_box.class)
							.toString());
		}
		rtnBeen.setObj(gNew_box);

		// Status check
		if (PROC_WAIT.equals(gNew_box.getBox_stat()) ||
				PROC_COMPLETE.equals(gNew_box.getBox_stat()) ||
				PROC_EMPTY.equals(gNew_box.getBox_stat()) ||
				(PROC_SHIP.equals(gNew_box.getBox_stat())
						&& !Util.isSpaceCheck(inTrx.getIn_bank())
						&& inTrx.getIn_bank().equals(_YES))) {
			;
		} else {
			return new SimpleRtnBeen(E_FSPBOXCHG_INVALID_NEW_BOX_STAT, "右侧箱号["
					+ inTrx.getBox_idB()+ "]状态为 [" + gNew_box.getBox_stat()
					+ "],状态无效!");
		}

		// Check real empt
		if (!PROC_EMPTY.equals(gNew_box.getBox_stat())
				&& EXIST_CARRIER.equals(gNew_box.getReal_emp())) {
			;// 非空BOX
		} else if (PROC_EMPTY.equals(gNew_box.getBox_stat())
				&& EMPTY_CARRIER.equals(gNew_box.getReal_emp())) {
			;// 真空BOX
		} else {
			return new SimpleRtnBeen(E_FSPBOXCHG_NEW_BOX_REAL_EMPT_CHK_NG,
					"右侧箱号真空状态检查NG, 箱子状态是[" + gNew_box.getBox_stat()
							+ "], 但真空标记(real_empt)是[" + gNew_box.getReal_emp()
							+ "]!");
		}
		return rtnBeen;
	}



	/**
	 * 检查原箱中prd信息
	 *
	 * @param //box_id
	 * @return
	 */
	private SimpleRtnBeen chkOldPrdInfo(FspboxchgI inTrx, Ret_box gOld_box) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		List<Ret_prd_info> gOld_prdList = retPrdInfoDao.getShtListInfoWithLock(inTrx.getBox_idA());
		if (null == gOld_prdList) {
			if (!PROC_EMPTY.equals(gOld_box.getBox_stat())) {
				return new SimpleRtnBeen(E_FSPBOXCHG_NO_PRD_IN_OLD_BOXG, "左侧箱号["
						+ inTrx.getBox_idA() + "]状态非空,但箱子中找不到产品ID! ");
			}
		} else {
			rtnBeen = mappingPrdInTrxWithDB(gOld_prdList, gOld_box, inTrx);
			if (_NORMAL != rtnBeen.getlRc()) {
				return rtnBeen;
			}
		}
		return rtnBeen;
	}

	/**
	 * 检查右侧箱子中prd信息
	 *
	 * @param //box_id
	 * @return
	 */
	private SimpleRtnBeen chkNewPrdInfo(FspboxchgI inTrx, Ret_box gNew_box) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);

		List<Ret_prd_info> gNew_prdList = retPrdInfoDao.getShtListInfoWithLock(inTrx
				.getBox_idB());
		if (null == gNew_prdList) {
			if (!PROC_EMPTY.equals(gNew_box.getBox_stat())) {
				return new SimpleRtnBeen(E_FSPBOXCHG_NO_PRD_IN_NEW_BOXG, "右侧箱号["
						+ inTrx.getBox_idB() + "]状态非空,但箱子中找不到产品ID! ");
			}
		} else {
			rtnBeen = mappingPrdInTrxWithDB(gNew_prdList, gNew_box, inTrx);
			if (_NORMAL != rtnBeen.getlRc()) {
				return rtnBeen;
			}
		}

		return rtnBeen;
	}

	/**
	 * 检查DB中查询到的Prd在inTrx中是否存在，存在则更新box id和slot no为inTrx中的值，不存在则报错
	 *
	 * @param prdList
	 * @param box
	 * @return SimpleRtnBeen {List<Ret_prd_info>}
	 */
	private SimpleRtnBeen mappingPrdInTrxWithDB(List<Ret_prd_info> prdList,
												Ret_box box, FspboxchgI inTrx) {
		int iO = 0, iN = 0;
		int iOLen = 0;
		int iNLen = 0;
		if (null != inTrx.getIaryA()) {
			iOLen = inTrx.getIaryA().size();
		}
		if (null != inTrx.getIaryB()) {
			iNLen = inTrx.getIaryB().size();
		}
		for (Ret_prd_info Prd : prdList) {
			for (iO = 0; iO < iOLen; iO++) {
				if (Prd.getPrd_seq_id().equals(inTrx.getIaryA().get(iO).getPrd_seq_id())) {
					//设置box_id
					Prd.setBox_id_fk(inTrx.getIaryA().get(iO).getBox_id_fk());
					//设置 slotNO
					Prd.setSlot_no(inTrx.getIaryA().get(iO).getSlot_no());
					break;
				}
			}
			for (iN = 0; iN < iNLen; iN++) {
				if (Prd.getPrd_seq_id().equals(inTrx.getIaryB().get(iN).getPrd_seq_id())) {
					Prd.setBox_id_fk(inTrx.getBox_idB());
					Prd.setSlot_no(inTrx.getIaryB().get(iN).getSlot_no());
					break;
				}
			}
			if ((iO < iOLen) && !(iN < iNLen) || !(iO < iOLen) && (iN < iNLen)) {
				;
			} else {
				return new SimpleRtnBeen(E_FSPBOXCHG_BOX_SLOT_CHK_FAILED,
						"产品ID[" + Prd.getPrd_seq_id() + "],箱号["
								+ box.getBox_id() + "] 子格位检查NG!");
			}
		}

		return new SimpleRtnBeen(prdList);
	}

	/**
	 * 更新Prd信息至目标箱子
	 *
	 * @param //old_prdList
	 * @param //new_prdList
	 */
	private long realChgPrdInfo(FspboxchgI inTrx, List<Ret_prd_info> gOld_prdList, List<Ret_prd_info> gNew_prdList) {
		Timestamp gEvtTimestamp = Util.getCurrentTimestamp();
		if (null != gOld_prdList) {
			for (Ret_prd_info prd : gOld_prdList) {
				prd.setEvt_cate(EVT_BOX_CHANGE);
				prd.setEvt_usr(inTrx.getEvt_usr());
				prd.setEvt_timestamp(gEvtTimestamp);
				prd.setSht_ope_msg(inTrx.getRemark());
				retPrdInfoDao.update(prd);
			}
		}

		if (null != gNew_prdList) {
			for (Ret_prd_info prd : gNew_prdList) {
				prd.setEvt_cate(EVT_BOX_CHANGE);
				prd.setEvt_usr(inTrx.getEvt_usr());
				prd.setEvt_timestamp(gEvtTimestamp);
				prd.setSht_ope_msg(inTrx.getRemark());
				retPrdInfoDao.update(prd);
			}
		}

		return _NORMAL;
	}

	/**
	 * 配置Box信息
	 */
	private long configBoxProc(Ret_box gOld_box, Ret_box gNew_box, FspboxchgO outTrx) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		String gszMsgBuf = "";
		rtnBeen = configBoxInfo(gOld_box);
		if (_NORMAL != rtnBeen.getlRc()) {
			gszMsgBuf = rtnBeen.getMsgBuf();
			outTrx.setRtn_mesg(gszMsgBuf);
			return rtnBeen.getlRc();
		}
		rtnBeen = configBoxInfo(gNew_box);
		if (_NORMAL != rtnBeen.getlRc()) {
			gszMsgBuf = rtnBeen.getMsgBuf();
			outTrx.setRtn_mesg(gszMsgBuf);
			return rtnBeen.getlRc();
		}
		return _NORMAL;
	}

	/**
	 * 检查箱子中玻璃信息是否一致
	 * @param box
	 * @return
	 */
	public SimpleRtnBeen configBoxInfo(Ret_box box) {
		SimpleRtnBeen simpleRtnBeen = new SimpleRtnBeen(_NORMAL);
		// Get prd list
		List<Ret_prd_info> gRet_prd_infoList = new ArrayList<Ret_prd_info>();
		gRet_prd_infoList = retPrdInfoDao.getShtListInfo(box.getBox_id());
		if (null == gRet_prd_infoList) {
			// EMPT BOX
			simpleRtnBeen.setObj(retBoxDao.setBoxEmpt(box));
		} else {
			/*if (gRet_prd_infoList.size() != box.getPrd_qty()) {
				simpleRtnBeen.setMsgBuf("箱子 [" + box.getBox_id() + "]的玻璃数量["
						+ box.getPrd_qty() + "]与装入的玻璃数量["
						+ gRet_prd_infoList.size() + "]不符!");
				simpleRtnBeen.setlRc(BOX_CNT_NOT_ENOUGT);
				return simpleRtnBeen;
			}*/

			Ret_wo ret_wo = retWODao.get(gRet_prd_infoList.get(0).getWo_id_fk());
			if(ret_wo == null){
				simpleRtnBeen.setMsgBuf("找不到箱子 [" + box.getBox_id() + "]的工单代码["
						+ gRet_prd_infoList.get(0).getWo_id_fk() + "]!");
				simpleRtnBeen.setlRc(E_RET_WO + E_READ_NOT_FOUND);
				return simpleRtnBeen;
			}

			// Check mix
			for (int i = 1; i < gRet_prd_infoList.size(); i++) {

				// Check status
				if (!gRet_prd_infoList.get(0).getPrd_stat()
						.equals(gRet_prd_infoList.get(i).getPrd_stat())) {
					simpleRtnBeen.setMsgBuf("状态不能混！ ["
							+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(0).getPrd_stat() + "] vs ["
							+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(i).getPrd_stat() + "]!");
					simpleRtnBeen.setlRc(SHT_STAT_MIX);
					return simpleRtnBeen;
				}

				if(!PROC_COMPLETE.equals(box.getBox_stat()) &&
						!PROC_SHIP.equals(box.getBox_stat())){
					// Check NX_PATH_ID
					if (gRet_prd_infoList.get(0).getNx_path_id_fk()!=null && !gRet_prd_infoList.get(0).getNx_path_id_fk()
							.equals(gRet_prd_infoList.get(i).getNx_path_id_fk())) {
						simpleRtnBeen.setMsgBuf("下一工艺路线不能混！["
								+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
								+ gRet_prd_infoList.get(0).getNx_path_id_fk()
								+ "] vs ["
								+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
								+ gRet_prd_infoList.get(i).getNx_path_id_fk()
								+ "]!");
						simpleRtnBeen.setlRc(SHT_PATH_ID_MIX);
						return simpleRtnBeen;
					}

					// Check NX_PATH_VER_FK
					if (gRet_prd_infoList.get(0).getNx_path_ver_fk()!=null&&!gRet_prd_infoList.get(0).getNx_path_ver_fk()
							.equals(gRet_prd_infoList.get(i).getNx_path_ver_fk())) {
						simpleRtnBeen.setMsgBuf("下一工艺路线版本不能混！["
								+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
								+ gRet_prd_infoList.get(0).getNx_path_ver_fk()
								+ "] vs ["
								+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
								+ gRet_prd_infoList.get(i).getNx_path_ver_fk()
								+ "]!");
						simpleRtnBeen.setlRc(SHT_PATH_VER_MIX);
						return simpleRtnBeen;
					}

					// Check NX_OPE_NO_FK
					if (gRet_prd_infoList.get(0).getNx_ope_no_fk()!=null&&!gRet_prd_infoList.get(0).getNx_ope_no_fk()
							.equals(gRet_prd_infoList.get(i).getNx_ope_no_fk())) {
						simpleRtnBeen
								.setMsgBuf("下一站点编号不能混！ ["
										+ gRet_prd_infoList.get(0).getPrd_seq_id()
										+ "]["
										+ gRet_prd_infoList.get(0)
										.getNx_ope_no_fk()
										+ "] vs ["
										+ gRet_prd_infoList.get(i).getPrd_seq_id()
										+ "]["
										+ gRet_prd_infoList.get(i)
										.getNx_ope_no_fk() + "]!");
						simpleRtnBeen.setlRc(SHT_OPE_NO_MIX);
						return simpleRtnBeen;
					}

					// Check NX_OPE_ID_FK
					if (gRet_prd_infoList.get(0).getNx_ope_id_fk()!=null&&!gRet_prd_infoList.get(0).getNx_ope_id_fk()
							.equals(gRet_prd_infoList.get(i).getNx_ope_id_fk())) {
						simpleRtnBeen
								.setMsgBuf("下一站点ID不能混！ ["
										+ gRet_prd_infoList.get(0).getPrd_seq_id()
										+ "]["
										+ gRet_prd_infoList.get(0)
										.getNx_ope_id_fk()
										+ "] vs ["
										+ gRet_prd_infoList.get(i).getPrd_seq_id()
										+ "]["
										+ gRet_prd_infoList.get(i)
										.getNx_ope_id_fk() + "]!");
						simpleRtnBeen.setlRc(SHT_OPE_ID_MIX);
						return simpleRtnBeen;
					}

					// Check NX_OPE_VER_FK
					if (gRet_prd_infoList.get(0).getNx_ope_ver_fk()!=null&&!gRet_prd_infoList.get(0).getNx_ope_ver_fk()
							.equals(gRet_prd_infoList.get(i).getNx_ope_ver_fk())) {
						simpleRtnBeen.setMsgBuf("下一站点版本不能混！ ["
								+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
								+ gRet_prd_infoList.get(0).getNx_ope_ver_fk()
								+ "] vs ["
								+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
								+ gRet_prd_infoList.get(i).getNx_ope_ver_fk()
								+ "]!");
						simpleRtnBeen.setlRc(SHT_OPE_VER_MIX);
						return simpleRtnBeen;
					}

					// Check NX_PROC_ID_FK
					if (gRet_prd_infoList.get(0).getNx_proc_id_fk()!=null&&!gRet_prd_infoList.get(0).getNx_proc_id_fk()
							.equals(gRet_prd_infoList.get(i).getNx_proc_id_fk())) {
						simpleRtnBeen.setMsgBuf("下一制程不能混！["
								+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
								+ gRet_prd_infoList.get(0).getNx_proc_id_fk()
								+ "] vs ["
								+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
								+ gRet_prd_infoList.get(i).getNx_proc_id_fk()
								+ "]!");
						simpleRtnBeen.setlRc(SHT_NX_PROC_ID_MIX);
						return simpleRtnBeen;
					}
				}

				if(!PROC_SHIP.equals(box.getBox_stat())){
					// Check WO_ID_FK
					if (!gRet_prd_infoList.get(0).getWo_id_fk()
							.equals(gRet_prd_infoList.get(i).getWo_id_fk())) {
						simpleRtnBeen.setMsgBuf("工单代码不能混！ ["
								+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
								+ gRet_prd_infoList.get(0).getWo_id_fk() + "] vs ["
								+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
								+ gRet_prd_infoList.get(i).getWo_id_fk() + "]!");
						simpleRtnBeen.setlRc(SHT_WO_ID_MIX);
						return simpleRtnBeen;
					}
				}


				// Check MDL_ID_FK
				if (!gRet_prd_infoList.get(0).getMdl_id_fk()
						.equals(gRet_prd_infoList.get(i).getMdl_id_fk())) {
					simpleRtnBeen.setMsgBuf("产品代码不能混！ ["
							+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(0).getMdl_id_fk()
							+ "] vs ["
							+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(i).getMdl_id_fk() + "]!");
					simpleRtnBeen.setlRc(SHT_MDL_ID_MIX);
					return simpleRtnBeen;
				}

				// Check BNK_FLG
				/*if (!gRet_prd_infoList.get(0).getBnk_flg()
						.equals(gRet_prd_infoList.get(i).getBnk_flg())) {
					simpleRtnBeen.setMsgBuf("入库标示不能混！ ["
							+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(0).getBnk_flg() + "] vs ["
							+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(i).getBnk_flg() + "]!");
					simpleRtnBeen.setlRc(SHT_BNK_FLG_MIX);
					return simpleRtnBeen;
				}
*/
			} // End of mix check

			// Check slot
			simpleRtnBeen = retBoxDao.slotCheck(gRet_prd_infoList);
			if (_NORMAL != simpleRtnBeen.getlRc()) {
				return simpleRtnBeen;
			}

			// set box info
			if (PROC_IN_PROCESS.equals(gRet_prd_infoList.get(0).getPrd_stat())) {
				box.setBox_stat(PROC_EMPTY);
			} else {
				box.setBox_stat(gRet_prd_infoList.get(0).getPrd_stat());
			}
			box.setReal_emp(EXIST_CARRIER);

			// Path info
			box.setNx_path_id_fk(gRet_prd_infoList.get(0).getNx_path_id_fk());
			box.setNx_path_ver_fk(gRet_prd_infoList.get(0).getNx_path_ver_fk());
			box.setNx_ope_no_fk(gRet_prd_infoList.get(0).getNx_ope_no_fk());
			box.setNx_ope_id_fk(gRet_prd_infoList.get(0).getNx_ope_id_fk());
			box.setNx_ope_ver_fk(gRet_prd_infoList.get(0).getNx_ope_ver_fk());
			box.setBox_size(String.valueOf(gRet_prd_infoList.size()));
			if("0".equals(box.getBox_size())){
				box.setBox_stat("EMPT");
			}
			// Prod & tool
			box.setNx_proc_id_fk(gRet_prd_infoList.get(0).getNx_proc_id_fk());
			box.setMdl_id_fk(gRet_prd_infoList.get(0).getMdl_id_fk());
			box.setMtrl_prod_id_fk(gRet_prd_infoList.get(0)
					.getMtrl_prod_id_fk());

			box.setNx_toolg_id_fk(gRet_prd_infoList.get(0).getNx_toolg_id_fk());
			box.setNx_tool_id_fk(_SPACE0);
			//下一站设备运行模式
			box.setNx_tool_run_mode(gRet_prd_infoList.get(0).getNx_tool_run_mode());

			box.setWo_id_fk(gRet_prd_infoList.get(0).getWo_id_fk());
			box.setBatch_no_fk(gRet_prd_infoList.get(0).getBatch_no());
			box.setBnk_flg(gRet_prd_infoList.get(0).getBnk_flg());
			box.setValid_flg(_YES);
			box.setPrty(gRet_prd_infoList.get(0).getPrty());
			box.setPv_prty(gRet_prd_infoList.get(0).getPv_prty());
			box.setDest_shop(gRet_prd_infoList.get(0).getDest_shop());
			box.setSo_id_fk(gRet_prd_infoList.get(0).getSo_id_fk());
			box.setPrd_qty(gRet_prd_infoList.size());
			box.setChip_qty(gRet_prd_infoList.get(0).getPrd_qty() * box.getPrd_qty());//面板计数
			if (null != gRet_prd_infoList.get(0).getPrd_admin_flg()){
				box.setPrd_admin_flg(gRet_prd_infoList.get(0).getPrd_admin_flg().substring(0, 1)+"NNNNNNNNNNNNNNNNNNN");
			}else{
				box.setPrd_admin_flg("NNNNNNNNNNNNNNNNNNNN");
			}
			box.setGroup_id(gRet_prd_infoList.get(0).getGroup_id());//更新箱子的group id
			simpleRtnBeen.setObj(box);

			retBoxDao.update(box);
		}

		return simpleRtnBeen;
	}
}
